package util;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class DataGenerator {
    public static void generate(String userDataPath, String productDataPath, String orderDataPath){
        String projectDir = System.getProperty("user.dir");
        String separator = File.separator;
        String dbLocation = projectDir + separator + "ds" + separator + "primary";
        File userTxt = new File(dbLocation + separator + userDataPath);
        File productTxt = new File(dbLocation + separator + productDataPath);
        File orderTxt = new File(dbLocation + separator + orderDataPath);
        FileOutputStream userFos;
        FileOutputStream productFos;
        FileOutputStream orderFos;
        try {
            File dir = new File(dbLocation);
            if(!dir.exists())
                dir.mkdirs();
            //判断文件是否存在，如果不存在就新建一个txt
            if(!userTxt.exists())
                userTxt.createNewFile();
            if(!productTxt.exists())
                productTxt.createNewFile();
            if(!orderTxt.exists())
                orderTxt.createNewFile();
            userFos = new FileOutputStream(userTxt);
            productFos = new FileOutputStream(productTxt);
            orderFos = new FileOutputStream(orderTxt);
//            fileOutputStream.write("txt".getBytes());
            productFos.write("#productId#productName#category#cost#price#deposit#unit\n".getBytes());
            userFos.write("userId;#userName#password;#email#tel#valid#createTime#shoppingAddress\n".getBytes());
            orderFos.write("orderId#productId#orderTime#userId#number#price#state\n".getBytes());
            Random random = new Random();
            int max = 10000000;
            for (int i = 0; i < max; i++) {
                Float cost = random.nextFloat() * 1000;
                Float price = cost + 100;
                String product = i + "#pro" + i + "#category" + random.nextInt(100) + "#" + cost + "#" + price + "#"+ random.nextInt(200) + "#pair\n";
                String user = i + "#user" + i + "#" + UUID.randomUUID() + "#" + (10000000 + i)+"@foxmail.com#" + "13477882345#" + random.nextInt(1) + "#" + new Date() + "#address" + i + "\n";
                String order = i + "#" + random.nextInt(max) + "#" + new Date() + "#" + random.nextInt(max) + "#" + (random.nextInt(10) + 1) + "#" + "pending\n";
                productFos.write(product.getBytes());
                userFos.write(user.getBytes());
                orderFos.write(order.getBytes());
            }
            productFos.flush();
            orderFos.flush();
            userFos.flush();
            productFos.close();
            userFos.close();
            orderFos.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
